var nameArr = [];
var valueArr = [];
var sum;
$(function () {
    // path = $("#path").val();
    salesWeek();
})


function salesWeek() {
    sum = 0;
    $.ajax({
        contentType: "application/json",
        url: "/salesWeek",
        method: "post",
        data: " ",
        dataType: "json",
        success: function (data) {
            nameArr = [];
            valueArr = [];
            nameArr = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"];
            // var  obj=JSON.parse(data);
            for (var i = 0; i < data.length; i++) {
                valueArr.push(data[i])
                sum += data[i] * 1;
            }
            sum = '本周商品销量总数:' + sum
            createSalesEchars();
            createSalesCurve();
        },
        error: function () {//状态不是200执行
            alert("网络繁忙");
        }
    })


}

function salesMonth() {
    sum = 0;
    $.ajax({
        contentType: "application/json",
        url: "/salesMonth",
        method: "post",
        data: " ",
        dataType: "json",
        success: function (data) {
            nameArr = [];
            valueArr = [];
            nameArr = ["第一周", "第二周", "第三周", "第四周", "第五周"];
            // var  obj=JSON.parse(data);
            for (var i = 0; i < data.length; i++) {
                valueArr.push(data[i])
                sum += data[i] * 1;
            }
            sum = '本月商品销量总数:' + sum
            createSalesEchars();
            createSalesCurve();
        },
        error: function () {//状态不是200执行
            alert("网络繁忙");
        }
    })
}


function salesHalfYear() {
    // alert("年的方法")
    sum=0;
    $.ajax({
        contentType: "application/json",
        url: "/salesYears",
        method: "post",
        data: " ",
        dataType: "text",
        success: function (data) {
            nameArr = [];
            valueArr = [];
            var obj=data.split("&&");

            var name=JSON.parse(obj[0]);
            for(var i=name.length-1;i>=0;i--){

                nameArr.push(name[i]);
            }
            var count=JSON.parse(obj[1])
            for(var i=0;i<count.length;i++){
                valueArr.push(count[i])
                sum+=count[i]*1;
            }
            sum='近半年商品销量总数:'+sum
            createSalesEchars();
            createSalesCurve();
        },
        error: function () {//状态不是200执行
            alert("网络繁忙");
        }
    })
    // sum = 0;
    // $.ajax({
    //     contentType: "application/json",
    //     url: "/salesYears",
    //     method: "post",
    //     data: " ",
    //     dataType: "text",
    //     success: function (data) {
    //         nameArr = [];
    //         valueArr = [];
    //         var obj = data.split("&&");
    //
    //         var name = JSON.parse(obj[0]);
    //         for (var i = name.length - 1; i >= 0; i--) {
    //
    //             nameArr.push(name[i]);
    //         }
    //         var count = JSON.parse(obj[1])
    //         for (var i = 0; i < count.length; i++) {
    //             valueArr.push(count[i])
    //             sum += count[i] * 1;
    //         }
    //         sum = '近半年商品销量总数:' + sum
    //
    //         createSalesEchars();
    //         createSalesCurve();
    //     },
    //     error: function () {//状态不是200执行
    //         alert("网络繁忙");
    //     }
    // })
}
function createSalesCurve() {

    var chartDom = document.getElementById('main2');
    var myChart = echarts.init(chartDom);
    var option;


    option = {
        xAxis: {
            type: 'category',
            data: nameArr,
        },
        yAxis: {
            type: 'value'
        },

        series: [{
            data: valueArr,
            type: 'line',
            smooth: true
        }]
    };

    option && myChart.setOption(option);

}


function createSalesEchars() {

// 基于准备好的dom，初始化echarts实例
    var myChart = echarts.init(document.getElementById('main'));

// 指定图表的配置项和数据
    var option = {
        title: {
            text: sum,
            x: '600'
            // offsetCenter:[0,50]
        },
        tooltip: {},
        legend: {
            data: ['商品销量']
        },
        xAxis: {
            data: nameArr
        },
        yAxis: {},
        series: [{
            name: '商品销量',
            type: 'bar',
            barWidth: 55,
            data: valueArr
        }]
    };

// 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
}


